package com.zhang.webadmin.exception;

import com.zhang.webadmin.util.R;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.authorization.AuthorizationDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(AuthorizationDeniedException.class)
    @ResponseStatus(code = HttpStatus.FORBIDDEN)
    public R handlerAuthorizationDeniedException(HttpServletRequest request, AuthorizationDeniedException e) {
//        log.error("req uri:{}", request.getRequestURI(), e);
        return R.fail("无权访问");
    }

    @ExceptionHandler(BizException.class)
    public R bizException(HttpServletRequest request, BizException e) {
        log.error("req uri:{}", request.getRequestURI(), e);
        return R.fail(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public R exception(HttpServletRequest request, Exception e) {
        log.error("req uri:{}", request.getRequestURI(), e);
        return R.fail("系统错误");
    }
}
